package com.jxbai.design.pattern.dcxy.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author jxbai
 * @date 2022/1/12 0012
 */
public class JdkTiktokProxy<T> implements InvocationHandler {

    private T target;

    public JdkTiktokProxy(T target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("真正执行被代理对象的方法");
        Object invoke = method.invoke(target, args);
        System.out.println("返回值：" + invoke);
        return invoke;
    }

    public static<T> T getProxy(T t) {
        Object o = Proxy.newProxyInstance(
                t.getClass().getClassLoader(),
                t.getClass().getInterfaces(),
                new JdkTiktokProxy<>(t)
        );
        return (T) o;
    }
}
